package com.atguigu.common.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2021/7/13 0013 10:40
 */
@Data
public class Result {

    private Integer code;
    private String  message;
    private Map<String,Object> data = new HashMap<>();

    private Result(){}

    // 成功
    public static Result ok(){

        Result r = new Result();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMessage(ResponseEnum.SUCCESS.getMessage());
        return  r;
    }

    // 失败
    public static Result error(){

        Result r = new Result();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMessage(ResponseEnum.ERROR.getMessage());
        return  r;
    }

    //返回带有数据
    public Result data(String key,Object value){
        this.data.put(key,value);
        return  this; // 返回实际调用这个方法的实例化对象
    }
    //返回带有数据
    public Result data(Map<String,Object> map){
        this.data = map;
        return this; // 返回实际调用这个方法的实例化对象
    }

    // 自定义消息
    public Result message(String message){
        this.message = message;
        return this; // 返回实际调用这个方法的实例化对象
    }

    // 自定义code
    public Result code(Integer code){
        this.code = code;
        return this; // 返回实际调用这个方法的实例化对象

    }

    public static Result setResult(ResponseEnum responseEnum){
        Result result = new Result();
        result.setMessage(responseEnum.getMessage());
        result.setCode(responseEnum.getCode());
        return  result;
    }






}
